package com.example.demo.clone_test;

class Address implements Cloneable {
    String city;
    String street;

    public Address(String city, String street) {
        this.city = city;
        this.street = street;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public String toString() {
        return "Address{" +
                "city='" + city + '\'' +
                ", street='" + street + '\'' +
                '}';
    }
}

class Person implements Cloneable {
    String name;
    int age;
    Address address;

    public Person(String name, int age, Address address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address=" + address +
                '}';
    }
}

public class Main {
    public static void main(String[] args) {
        try {
            Address address = new Address("New York", "5th Avenue");
            Person person1 = new Person("John", 30, address);
            Person person2 = (Person) person1.clone();

            System.out.println("Before modification:");
            System.out.println("Person1: " + person1);
            System.out.println("Person2: " + person2);

            // Modify the address of person2
            person2.address.city = "Los Angeles";
            person2.address.street = "Sunset Boulevard";

            System.out.println("After modification:");
            System.out.println("Person1: " + person1);
            System.out.println("Person2: " + person2);

        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}
